package com.lalameow.researchstation.mixin;

import com.lalameow.researchstation.api.technology.Technology;
import com.lalameow.researchstation.util.mixin.MutableResearching;
import com.lalameow.researchstation.util.mixin.ResearchingHelper;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.Identifier;
import net.minecraft.world.World;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.Set;

@Mixin(PlayerEntity.class)
public abstract class ResearchingPlayerMixin extends LivingEntity implements MutableResearching {
    private ResearchingPlayerMixin(EntityType<? extends LivingEntity> entityType, World world) {
        super(entityType, world);
    }

    private PlayerEntity cast() {
        return (PlayerEntity) (Object) this;
    }

    @Inject(method = "initDataTracker", at = @At("TAIL"))
    private void researching$initDataTracker(CallbackInfo ci) {
        ResearchingHelper.INSTANCE.initDataTracker(cast());
    }

    @Override
    public @NotNull Set<Technology> getResearched() {
        return ResearchingHelper.INSTANCE.getResearched(cast());
    }

    @Override
    public void research(@NotNull Technology technology) {
        getTechnologies().add(technology.getId());
    }

    @NotNull
    @Override
    public Set<Identifier> getTechnologies() {
        return ResearchingHelper.INSTANCE.getTechnologies(cast());
    }

    public void setTechnologies(Set<Identifier> technologies) {
        ResearchingHelper.INSTANCE.setTechnologies(cast(), technologies);
    }

    @Inject(method = "writeCustomDataToNbt", at = @At("TAIL"))
    private void researching$writeCustomDataToNbt(CallbackInfo ci) {
        ResearchingHelper.INSTANCE.write(cast());
    }

    @Inject(method = "readCustomDataFromNbt", at = @At("TAIL"))
    private void researching$readCustomDataFromNbt(CallbackInfo ci) {
        ResearchingHelper.INSTANCE.read(cast());
    }
}
